import { createWebHistory, createRouter } from "vue-router";
import { getToken } from "../utils/token";
import axios from "axios";
export const constantRoutes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("@/view/home/index"),
    name: "home",
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/dynamic",
    component: () => import("@/view/dynamic/index"),
    name: "dynamic",
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/server",
    component: () => import("@/view/server/index"),
    name: "server",
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/mine",
    component: () => import("@/view/mine/index"),
    name: "mine",
  },
  {
    path: "/companyInfo",
    component: () => import("@/view/companyInfo/index"),
  },
  {
    path: "/txtInfo/:id",
    component: () => import("@/view/txtInfo/index"),
  },
  {
    path: "/queryDetails/:id",
    component: () => import("@/view/queryDetails/index"),
  },
  {
    path: "/questionDetail/:id",
    component: () => import("@/view/questionDetail/index"),
  },
  {
    path: "/queryList",
    component: () => import("@/view/queryList/index"),
  },
  {
    path: "/queryInfo/:id",
    component: () => import("@/view/queryInfo/index"),
  },
  {
    path: "/questionList",
    component: () => import("@/view/questionList/index"),
  },
  {
    path: "/questionInfo/:id",
    component: () => import("@/view/questionInfo/index"),
  },
  {
    path: "/userInfo",
    component: () => import("@/view/userInfo/index"),
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});
router.beforeEach(async (to, from, next) => {
  // localStorage.setItem('companyCode','d35abd9264b24c1abbe0715fa5c6ed40')
  if(localStorage.getItem('companyCode')){
    next();
    return
  }
  await axios('/').then(res=>{
    localStorage.setItem('companyCode',res.headers['x-my-env-var'])
  })
  next();
});

export default router;
